<template>
  <div style="height:670px;background: #f6f7f9;">
    <!-- 顶部导航 -->
    <van-sticky>
      <van-nav-bar title="客户评价" left-arrow @click-left="$router.go(-1)">
      </van-nav-bar>
    </van-sticky>
    <van-list
        :finished="finished"
        finished-text="没有更多了"
    >
      <div v-for="item in list">
        <van-cell title="单元格" :value="item.userDate" style="text-align: left; margin-top: 10px;">
          <template #title>
            <div style="font-size: 16px; font-weight: bold;">订单号：{{ item.orderSn }}</div>
          </template>
          <template #label>
            <div style="color: #333333; width: 180px;">
              用户评分：
              <van-rate v-model="item.userScore" color="#ffd21e" readonly/>
            </div>
            <div style="color: #333333; width: 250px; font-size: 14px; margin-top: 15px;">
              {{ item.userEvaluate }}
            </div>
            <div v-if="item.addMessage!=null" style="color: #333333; width: 250px; font-size: 14px; margin-top: 15px;">
              <span style="font-weight: bold">追评:</span>
              <van-field
                  v-model="item.addMessage"
                  rows="2"
                  autosize
                  style="background: #eeeeee; border-radius: 10px; text-align: left;"
                  type="textarea"
                  readonly
                  show-word-limit
              />
            </div>
            <van-icon name="delete-o" size="20" @click="deleteEvaluate(item.id)" style="margin-top: 20px;"/>
            <span style="font-size: 13px;">删除</span>
          </template>
        </van-cell>
      </div>
    </van-list>
  </div>
</template>

<script>
export default {
  name: "OrderEvaluateList",
  data() {
    return {
      list: [
        {
          id: '',
          orderSn: '2301281022221522749',
          userScore: 3,
          userEvaluate: '',
          addMessage:'',
          userDate: '2023-01-12',
        }
      ],
      finished: false,
    };
  },
  methods: {
    loadOrderEvaluateList() {
      console.log('在localStorage中的JWT数据：' + localStorage.getItem('jwt'));
      let url = 'http://localhost:9080/evaluates/staff';
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        console.log(response.data.data);
        let responseBody = response.data.data;
        for (let i = 0; i < responseBody.length; i++) {
          responseBody[i].userScore = parseInt(responseBody[i].userScore / 3);
          responseBody[i].userDate = /\d{4}-\d{1,2}-\d{1,2}/g.exec(responseBody[i].userDate)[0];
        }
        this.list = responseBody;
      })
    },

    deleteEvaluate(id) {
      this.$dialog.confirm({
        title: '删除评价',
        message: '删除后不可恢复，是否继续？',
      })
          .then(() => {
            let url = 'http://localhost:9080/evaluates/'+id+'/delete';
            this.axios
                .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
                .post(url).then((response) => {
              let responseBody = response.data;
              console.log(responseBody);
              if (responseBody.state == 20000) {
                this.$toast.success('删除评价成功!');
                this.loadOrderEvaluateList();
              }else{
                this.$toast.fail(responseBody.message);
              }
            });
          })
          .catch(() => {
            // on cancel
          });
    }
  },
  mounted() {
    this.loadOrderEvaluateList();
  }
}
</script>

<style scoped>

</style>
